﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;
using Utilities.QuickSql;

namespace WindowsFormsApplication1
{
    public partial class gest_modele : Form
    {
        MySqlDatabase db = new MySqlDatabase("localhost", "B2_CS2I", "motdepasse", "projets4");
        

        public gest_modele()
        {
            InitializeComponent();
            textBox_NomModele.Visible = false;
            textBox_Annee.Visible = false;
            textBox_Prix.Visible = false;

            dataGrid_modele.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            
        }

        private void gest_modele_Load(object sender, EventArgs e)
        {
            // TODO: cette ligne de code charge les données dans la table 'projets4DataSet.modele'. Vous pouvez la déplacer ou la supprimer selon vos besoins.
            this.modeleTableAdapter.Fill(this.projets4DataSet.modele);

        }

        public void dataGrid_modele_CellClick(object sender, DataGridViewCellEventArgs e)
        {           

            Table modele = db.QueryTable("SELECT * FROM modele WHERE id_modele = '" + dataGrid_modele.SelectedRows[0].Cells[0].Value.ToString() + "'");
            foreach (Record r in modele)
            {
                this.textBox_NomModele.Text = (string)r.Values[1];
                this.textBox_Annee.Text = r.Values[2].ToString();
                this.textBox_Prix.Text = r.Values[3].ToString();
                this.pictureBox1.ImageLocation = r.Values[6].ToString();
            }
            
        }

        private void button_ok_Click(object sender, EventArgs e)
        {
            string modif = db.QueryValue<string>("UPDATE modele SET nom_modele ='" + textBox_NomModele.Text.ToString() + "', annee = '" + textBox_Annee.Text.ToString() + "', prix_base = '" + textBox_Prix.Text.ToString() + "' WHERE id_modele ='" + dataGrid_modele.SelectedRows[0].Cells[0].Value.ToString() + "'");
            
            this.modeleTableAdapter.Fill(this.projets4DataSet.modele);
        }

        private void button_modifier_Click(object sender, EventArgs e)
        {
            textBox_NomModele.Visible = true;
            textBox_Annee.Visible = true;
            textBox_Prix.Visible = true;
        }

        private void button_suppr_Click(object sender, EventArgs e)
        {
            string suppr = db.QueryValue<string>(" DELETE  FROM modele WHERE id_modele ='" + dataGrid_modele.SelectedRows[0].Cells[0].Value.ToString() + "'");
            this.modeleTableAdapter.Fill(this.projets4DataSet.modele);   
        }

        private void button_add_Click(object sender, EventArgs e)
        {
            new_vehicule fenNewVehicule = new new_vehicule();
            fenNewVehicule.Owner = this;
            fenNewVehicule.Show();
        }
    }
}
